package com.message;

import com.game.gm.handler.Handler;
import com.game.gm.handler.ResGMmsgHandler;
import com.game.gm.message.ResGMmsgMessage;
import game.message.Message;
import java.util.HashMap;

public class MessagePool {
	private static MessagePool instance = new MessagePool();

	private HashMap<Integer, Class<? extends Handler>> id2handler = new HashMap();
	private HashMap<Integer, Class<? extends Message>> id2message = new HashMap();

	public static MessagePool getInstance() {
		return instance;
	}

	public void register(int id, Class<? extends Handler> handlerClass, Class<? extends Message> messageClass) {
		this.id2handler.put(Integer.valueOf(id), handlerClass);
		this.id2message.put(Integer.valueOf(id), messageClass);
	}

	public Handler createHandler(int id) {
		Class handlerClass = (Class) this.id2handler.get(Integer.valueOf(id));
		if (handlerClass == null) {
			return null;
		}
		Handler handler = null;
		try {
			handler = (Handler) handlerClass.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return handler;
	}

	public Message createMessage(int id) {
		Class messageClass = (Class) this.id2message.get(Integer.valueOf(id));
		if (messageClass == null) {
			return null;
		}
		Message message = null;
		try {
			message = (Message) messageClass.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return message;
	}

	private MessagePool() {
		register(203201, ResGMmsgHandler.class, ResGMmsgMessage.class);
	}
}